<?php
	
	class SmRoot0v0r01_User extends _smuoiDbRowExtended implements SmRoot_User{
	
		protected $table = 'users';
	
		protected $get_functions = 'is_root,is_sudoer,is_logged_out,grp,groups,can_script,can_apache,can_symlink,session_data';
		protected $set_functions = 'home,name,email,pass,groups,class,session_data,sudo_perms,can_script,can_apache,can_symlink';
		
		protected $flag_ok, $session;
			
		function get($k){
			if($k == 'password') return;
			return parent::get($k);
		}
		
		function data(){
			$o = new obj($this->data);
			unset($o->password);
			return $o;
		}
	
####	Logging in/out

		function ok(){
			return $this->flag_ok;	
		}
		
		function pass_check($sha1, $salt){
			// should never happen, but anyway
			if(!$salt = trim((string)$salt)) return false;
			if(!$pass = trim((string)$this->data->password)) return false;
			if($this->flag_ok = ($sha1 == sha1($pass.$salt))){
				if(!$this->session){	
					require_once 'Session.php';
					$this->session = new SmRoot0v0r01_Session(false, $this->pkg, false, $this);
				}else $this->session->user_reset($this);
				return $this->session;
			}
			return false;
		}

		function continue_session($ses){
			$this->flag_ok = ($ses->ok() && ($ses->usr() === $this));
			if($this->flag_ok) $this->session = $ses;	
		}

####	Getters
		
		function domain(){
			return Domain($this->data->home);	
		}
			
		function is_root(){
			return false;
		}
		
		function is_sudoer(){
			return false;
		}
				
		function grp(){
			return $this->pkg->primary_of($this)->id;
		}
		
		function group(){
			return $this->pkg->primary_of($this);
		}
		
		function groups(){
			return explode(',', $this->data->groups);
		}
		
		function can_script(){
			return false;
		}

		function can_apache(){
			return false;
		}
		
		function can_symlink(){
			return false;
		}
		
####	Setters
		
		function join_group($g){
			if(!$g instanceof SmRoot_Group) return false;
			if(!$g->accept_member($this)) return false;
			$grps = $this->groups();
			$grps[] = $g->id;
			// Update data->groups so changes to e.g. home in same save will permit access
			$this->hard_set('groups', implode(',', array_unique($grps)));
			return true;
		}
		
		function leave_group($g){
			if(!$g instanceof SmRoot_Group) return false;	
			$grps = $this->groups();
			foreach($grps as $n=>$id) if($id == $g->id) unset($grps[$n]);
			$this->hard_set('groups', implode(',', array_unique($grps)));
			return true;
		}
		
		protected function set_home($h){
			if(!$h || (!$dom = Domain($h))) return false;
			if($h == $this->data->home) return true;
			if(!$dom->will_permit($this)) return false;
			return $this->hard_set('home', $h);
		}
			
		protected function set_name($n){
			if(!$n = preg_replace('/[^A-Za-z0-9]/', '', $n)) return false;
			if($n == $this->data->name) return false;
			if($this->pkg->al()->exists($this->table(), array('name'=>$n))) return false;
			return $this->hard_set('name', $n);
		}
		
		protected function set_email($e){
			if($e == $this->data->email) return false;
			if(!is_valid_email($e)) return false;	
			if($this->pkg->al()->exists($this->table(), array('email'=>$e))) return false;
			return true;
		}
		
		protected function set_pass($pass){
			if(strlen($pass) < 40) return false;
			$this->hard_set('password', $pass);
			return true;
		}
		
		protected function set_groups($groups){
			if(!is_array($groups)) $groups = explode(',', $groups);
			$ids = array($this->grp());
			foreach($groups as $id){
				if(!$grp = $this->db($id)) continue;
				if(!$grp->accept_member($this)) continue;
				$ids[] = $id;
			}
			$this->hard_set('groups', implode(',', array_unique($ids)));
			return true;
		}
		
		protected function set_class($class){
			if(!smuoi_is_root(usr()) || !in_array($class, array('User', 'Sudoer'))) return false;
			return true;
		}
		
		protected function set_sudo_perms(){
			return false;
		}
		
		protected function set_can_script(){
			return false;
		}

		protected function set_can_apache(){
			return false;
		}
		
		protected function set_can_symlink(){
			return false;
		}
		
	}

?>
